import { message } from 'antd';
import {
  getDictList,
  getOrgList,
  getOrgChildList,
  getPageDataList,
  insertInfo,
  deleteOrgById,
  queryOrgById,
  updateOrgInfo,
  getAreaList,
} from '@/services/authoritymanage';

export default {
  state: {
    treeData: [],
    selectedTreeKeys: [],
    expandedTreeKeys: [],
    addModVisible: false,
    isAdd: true, // true:新增 false:修改
    pageData: {
      list: [],
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
      },
    },
    dictData: {
      ORG_TYPE: [],
    },
    parentId: '',
    corpId: '',
    modifyData: {},
    areaList: [],
    areaShow: false,
  },
  effects: {
    // 获取字典
    *fetchDictList({ payload }, { call, put }) {
      const response = yield call(getDictList, payload);
      if (response.success) {
        yield put({
          type: 'getDictData',
          payload: response.data,
        });
      } else {
        message.error(response.message);
      }
    },
    *fetchOrgList({ payload, callback = () => {} }, { call, put }) {
      const response = yield call(getOrgList, payload);
      if (response.success) {
        callback(response.data[0].orgId);
        yield put({
          type: 'getTreeData',
          payload: response.data,
        });
      } else {
        message.warning(`${response.message || '数据查询失败！'}`);
      }
    },
    *fetchOrgListByParentId({ payload }, { call, put }) {
      const { parentId } = payload;
      const response = yield call(getOrgChildList, payload);
      if (response.success) {
        yield put({
          type: 'getChildData',
          payload: { data: response.data, parentId },
        });
      } else {
        message.warning(`${response.message || '数据查询失败！'}`);
      }
    },
    *fetchPageListByParentId({ payload }, { call, put }) {
      const response = yield call(getPageDataList, payload);
      const {
        page: { currentPage, pageSizes },
      } = payload;
      if (response.success) {
        yield put({
          type: 'getPageDataList',
          payload: {
            data: response.data,
            currentPage,
            pageSize: pageSizes,
          },
        });
      } else {
        message.warning(`${response.message || '数据查询失败！'}`);
      }
    },
    // 新增
    *insertOrgInfo({ payload, callback }, { call }) {
      const response = yield call(insertInfo, payload);
      if (response.success) {
        callback(true);
      } else {
        message.warning(`${response.message || '新增失败！'}`);
      }
    },

    // 修改前查询修改信息
    *fetchOrgById({ payload }, { call, put }) {
      const response = yield call(queryOrgById, payload);
      if (response.success) {
        yield put({
          type: 'getModifyData',
          payload: response.data,
        });
      } else {
        message.warning(`${response.message || '查询失败！'}`);
      }
    },
    // 修改
    *updateOrgInfo({ payload, callback }, { call }) {
      const response = yield call(updateOrgInfo, payload);
      if (response.success) {
        callback(true);
        message.success(`${response.message || '修改成功！'}`);
      } else {
        message.warning(`${response.message || '修改失败！'}`);
      }
    },
    // 删除
    *deleteOrg({ payload, callback }, { call }) {
      const response = yield call(deleteOrgById, payload);
      if (response.success) {
        callback(true);
      } else {
        message.warning(`${response.message || '删除失败！'}`);
      }
    },
    // 获取服务地区列表
    *fetchAreaList({ payload, callback }, { call, put }) {
      const response = yield call(getAreaList, payload);
      if (response.success) {
        if (callback) {
          callback(true);
        }
        yield put({
          type: 'getAreaList',
          payload: response.data,
        });
      } else {
        message.warning(`${response.message || '删除失败！'}`);
      }
    },
  },

  reducers: {
    // 获取字典数据
    getDictData(state, { payload }) {
      return {
        ...state,
        dictData: payload,
      };
    },
    // 获取树形数据
    getTreeData(state, { payload }) {
      const treeData = payload.map(item => ({
        title: item.name,
        key: item.orgId,
        corpId: item.corpId,
        isLeaf: item.leafFlag !== 2,
      }));
      return {
        ...state,
        treeData,
        selectedTreeKeys: [payload[0].orgId],
        expandedTreeKeys: [payload[0].orgId],
        corpId: payload[0].corpId,
      };
    },
    getChildData(state, { payload }) {
      if (state.treeData.length) {
        const childData = payload.data.map(item => ({
          title: item.name,
          key: `${item.orgId}`,
          corpId: item.corpId,
          dataInfo: item,
          isLeaf: item.leafFlag !== 2,
        }));
        const treeData = data => {
          data.forEach(item => {
            if (item.children) {
              treeData(item.children);
            }
            if (item.key === payload.parentId) {
              // eslint-disable-next-line no-param-reassign
              item.children = childData;
            }
          });
        };

        treeData(state.treeData);
      }

      return {
        ...state,
        treeData: state.treeData,
      };
    },
    // 设置修改当前state数据
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    getModifyData(state, { payload }) {
      const list = payload.areaList && Array.isArray(payload.areaList) ? payload.areaList : [];
      const areaList = list.map(item => ({
        key: item.areaId,
        label: item.areaName,
      }));
      return {
        ...state,
        modifyData: { ...payload, areaList },
        areaList: state.areaList.concat(payload.areaList),
        // modifyData: payload,
      };
    },
    getPageDataList(
      state,
      {
        payload: { data, currentPage, pageSize },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        pageData: {
          list,
          pagination: { total: data.totalCount || 0, current: currentPage, pageSize },
        },
      };
    },
    getAreaList(state, { payload }) {
      return {
        ...state,
        areaList: payload,
      };
    },
  },
};
